/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.service.impl;

import cz.muni.fi.pa165.machineryrental.converters.CustomerDTOConverter;
import cz.muni.fi.pa165.machineryrental.dao.CustomerDAO;
import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.entities.Customer;
import cz.muni.fi.pa165.machineryrental.service.CustomerService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Jozef Triscik
 */
@Service("CustomerService")
@Component
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDAO customerDAO;

    public void setCustomerDAO(CustomerDAO customerDAO) {
        if (customerDAO == null) {
            throw new IllegalArgumentException("Given customer is null.");
        }

        this.customerDAO = customerDAO;
    }

    @Transactional
    public CustomerDTO getById(long id) {

        Customer customer = this.customerDAO.getById(id);
        return customer == null ? null : CustomerDTOConverter.toDTO(customer);
    }

    @Transactional
    public void create(CustomerDTO customer) {
        if (customer == null) {
            throw new IllegalArgumentException("Given customer cannot be null");
        }

        Customer customerEntity = CustomerDTOConverter.toEntity(customer);
        this.customerDAO.create(customerEntity);
        CustomerDTOConverter.applyToDTO(customer, customerEntity);
    }

    @Transactional
    public void update(CustomerDTO customer) {
        if (customer == null) {
            throw new IllegalArgumentException("Given customer cannot be null");
        }

        this.customerDAO.update(CustomerDTOConverter.toEntity(customer));
    }

    @Transactional
    public void remove(CustomerDTO customer) {
        if (customer == null) {
            throw new IllegalArgumentException("Given customer cannot be null");
        }

        this.customerDAO.remove(CustomerDTOConverter.toEntity(customer));
    }

    @Transactional
    public List<CustomerDTO> findAll() {
        return CustomerDTOConverter.toDTOList(this.customerDAO.findAll());
    }

    @Transactional
    public List<CustomerDTO> findByLastName(String lastName) {
        return CustomerDTOConverter.toDTOList(this.customerDAO.findByLastName(lastName));
    }

    @Transactional
    public CustomerDTO findByEmail(String email) {
        Customer customer = this.customerDAO.findByEmail(email);
        return customer == null ? null : CustomerDTOConverter.toDTO(customer);
    }

    @Transactional
    public CustomerDTO findByCompanyNumber(String companyNumber) {
        Customer customer = this.customerDAO.findByCompanyNumber(companyNumber);
        return customer == null ? null : CustomerDTOConverter.toDTO(customer);
    }

    @Transactional(readOnly = true)
    public boolean isEmailUnique(String email) {
        return customerDAO.isEmailUnique(email);
    }

    @Transactional(readOnly = true)
    public boolean isVATNumberUnique(String vatNumber) {
        return this.customerDAO.isVATUnique(vatNumber);
    }

    @Transactional(readOnly = true)
    public boolean isCompanyNumberUnique(String companyNumber) {
        return this.customerDAO.isCompanyNumberUnique(companyNumber);
    }
}
